﻿using MediatR;
using W.Purchase.API.Application.Command;
using W.Purchase.Domain;
using W.Purchase.Repository.Interface;

namespace W.Purchase.API.Application.CommandHandler
{
    public class VendorAddCommandHandler : IRequestHandler<VendorAddCommand, int>
    {
        private readonly IVendorRepository vendorRepository;

        public VendorAddCommandHandler(IVendorRepository vendorRepository)
        {
            this.vendorRepository = vendorRepository;
        }

        public async Task<int> Handle(VendorAddCommand request, CancellationToken cancellationToken)
        {
            var list = (await vendorRepository.GetAllAsaync()).Where(w => w.VendorName == request.VendorName);
            if(list.Count()!=0)
            {
                return -1;
            }
            else
            {
                request.IsDelete = false;
                return await vendorRepository.AddAsync(new Domain.Vendor() { VendorId = request.VendorId, VendorNo = request.VendorNo, VendorName = request.VendorName, VendorState = request.VendorState, VendorTypeId = request.VendorTypeId, VendorPhone = request.VendorPhone, VendorFax = request.VendorFax, VendorEmail = request.VendorEmail, VendorContact = request.VendorContact, VendorAddress = request.VendorAddress, VendorDesc = request.VendorDesc, VendorRemarks = request.VendorRemarks, office_id = request.office_id, user_id = request.user_id, CreateName = request.CreateName, CreateTime = request.CreateTime, UpdateName = request.UpdateName, UpdateTime = request.UpdateTime, DeleteName = request.DeleteName, DeleteTime = request.DeleteTime, IsDelete = request.IsDelete });
            }
           
        }
    }
}
